package com.paireach.scmc.http;

import android.content.Context;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.TextHttpResponseHandler;
import com.paireach.scmc.utils.GsonUtil;
import com.paireach.scmc.utils.LogUtil;
import com.paireach.scmc.utils.ProgressDialogUtil;
import com.paireach.scmc.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

/**
 * Created by ${cqc} on 2017/8/15.
 */

public class HttpUtil {


    private static ProgressDialogUtil dialogUtil;

    public static <T> void getData(Context context, String module, String operation, Map<String, String> map, final Class<T> clazz, final Boolean isNeedDialog, final HttpCallBack<T> callBack) {

        dialogUtil = new ProgressDialogUtil(context);
        if (isNeedDialog) {
            dialogUtil.showDialog();
        }
        HttpClientUtil.postJson(module, operation, map, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogUtil.d("onFailure=" + responseString + ",throwable=" + throwable.toString());
                if (isNeedDialog) {
                    dialogUtil.showDialog();
                }
                dialogUtil.dismissDialog();
                callBack.onFail(throwable.toString());
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                LogUtil.d("onSuccess=" + responseString);
                if (isNeedDialog) {
                    dialogUtil.showDialog();
                }
                try {
                    JSONObject obj = new JSONObject(responseString);
                    boolean resultFlag = obj.getBoolean("resultFlag");
                    if (resultFlag) {
                        Object entity = obj.get("responseEntity");
                        if (entity instanceof JsonArray) {
                            LogUtil.d("entity instanceof JsonArray");
                        } else if (entity instanceof JSONObject){
                            LogUtil.d("entity instanceof JSONObject");
                        } else if (entity instanceof String){
                            LogUtil.d("entity instanceof String");
                        }else{
                            LogUtil.d("other type");
                        }
                        if (entity instanceof JSONObject) {
                            T t1 = GsonUtil.jsonToBean(entity.toString(), clazz);
                            List<T> list = new ArrayList<T>();
                            list.add(t1);
                            callBack.onSuccess(list);

                        } else if (entity instanceof JsonArray) {
                            //如果是："responseEntity":[]，那么不是JsonArray类型
                            if (((JsonArray) entity).size() == 0) {
                                callBack.onFail("暂无数据");
                            } else {
                                List<T> list = (List<T>) GsonUtil.jsonToList(entity.toString(), new TypeToken<List<T>>() {
                                }.getType());
                                callBack.onSuccess(list);
                            }
                        } else {
//                            callBack.onFail("其他类型");
                            callBack.onFail("没有数据");
                        }
                    } else {
                        callBack.onFail("数据解析错误");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    callBack.onFail("数据解析错误");
                }
            }
        });
    }


}
